package DAO;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;

import org.apache.log4j.Logger;

import denisLunchy.*;

public class DBOrderDao implements ADao {
	String user;
	String password;
	String url;
	String driver;
	Connection c = null;
	Properties dbProp = new Properties();
	DataInputStream is = null;
	Logger log;

	public DBOrderDao() {
		// TODO Auto-generated constructor stub
		log = Logger.getLogger("simple");
		try {
			is = new DataInputStream(new FileInputStream("DBprop.txt"));
			dbProp.load(is);
			is.close();
		} catch (FileNotFoundException e2) {
			// TODO Auto-generated catch block
			log.error("Error", e2);
			e2.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			log.error("Error", e1);
			e1.printStackTrace();
		}
		user = dbProp.getProperty("user");
		password = dbProp.getProperty("pass");
		url = dbProp.getProperty("url");
		driver = dbProp.getProperty("driver");
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void save(Object object) {
		// TODO Auto-generated method stub
		Order order = (Order) object;
		int idOrd = order.getId();
		int idDish;
		String nameCl = order.getClient().getNameClient();
		String srNameCl = order.getClient().getSurName();
		String name;
		String type;
		int cost;
		int count;
		c = null;
		try {
			c = DriverManager.getConnection(url, user, password);
			Statement st = c.createStatement();
			for (Dish dish : order.arrayDish) {
				name = dish.getName();
				cost = dish.getCost();
				count = dish.getCount();
				type=dish.getType();
				st.executeUpdate("INSERT INTO order1 (id_order,name_Client,Surname_Client,name_dish, cost_dish, count_dish,type_dish) VALUES("
						+ idOrd
						+ ",'"
						+ nameCl
						+ "','"
						+ srNameCl
						+ "','"
						+ name + "'," + cost + "," + count + ",'"+type+"')");
			}

		} catch (Exception e) {
			log.error("Error", e);
			e.printStackTrace();
		} finally {
			try {
				if (c != null)
					c.close();
			} catch (SQLException e) {
				log.error("Error", e);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public LunchyClass getObject(int id) {
		// TODO Auto-generated method stub
		Order order = new Order();
		Client client = new Client();
		Dish dish;
		int index = 0;
		order.setId(id);
		c = null;
		try {
			c = DriverManager.getConnection(url, user, password);
			Statement st = c.createStatement();
			ResultSet rs = st
					.executeQuery("SELECT * FROM order1 WHERE id_order=" + id);
			while (rs.next()) {
				dish = new Dish();
				client.setNameClient(rs.getString(3));
				client.setSurName(rs.getString(4));
				dish.setId(Integer.parseInt(rs.getString(2)));
				dish.setName(rs.getString(5));
				dish.setCost(Integer.parseInt(rs.getString(6)));
				dish.setCount(Integer.parseInt(rs.getString(7)));
				dish.setType(rs.getString(8));
				order.addItem(dish);
			}
		} catch (Exception e) {
			log.error("Error", e);
			e.printStackTrace();
		} finally {
			try {
				if (c != null)
					c.close();
			} catch (SQLException e) {
				log.error("Error", e);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		order.setClient(client);
		return order;
	}

	@Override
	public void update(Object object) {
		// TODO Auto-generated method stub

	}

	@Override
	public void remove(int id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		c = null;
		try {
			c = DriverManager.getConnection(url, user, password);
			Statement st = c.createStatement();
			st.executeUpdate("DELETE FROM order1");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (c != null)
					c.close();
			} catch (SQLException e) {
				log.error("Error", e);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	@Override
	public ArrayList<Object> getAll() {
		ArrayList<Object> Menu = new ArrayList<Object>();
		Order order = null;
		ArrayList<String> clients=new ArrayList<String>();
		String nameCl = null;
		String srNameCl = null;
		Dish dish;
		Client client;
		c = null;
		try {
			c = DriverManager.getConnection(url, user, password);
			Statement st = c.createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM order1");
			while (rs.next()) {
				if (!clients.contains(rs.getString(3))){
				clients.add(rs.getString(3));}
			};
			for (String name:clients){
			ResultSet rs2 = st.executeQuery("SELECT * FROM order1 WHERE name_client='"+name+"'");
			order=new Order();
			while(rs2.next()){
				order.setId(Integer.parseInt(rs2.getString(1)));
				order.setClient(order.addClient(rs2.getString(3),rs2.getString(4)));
				dish=new Dish();
				dish.setId(Integer.parseInt(rs2.getString(2)));
				dish.setName(rs2.getString(5));
				dish.setCost(Integer.parseInt(rs2.getString(6)));
				dish.setCount(Integer.parseInt(rs2.getString(7)));
				dish.setType(rs2.getString(8));
				order.addItem(dish);
			}
			Menu.add(order);
			
			}
		
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (c != null)
					c.close();
			} catch (SQLException e) {
				log.error("Error", e);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		/*	Order ord;
			for (Object order1: Menu){
				ord=(Order) order1;
				System.out.println(ord.getClient().getSurName());
			}*/
		}
		return Menu;
	}

}
